import Vue from 'vue'
import VueRouter from 'vue-router'

Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    redirect: '/login'
  },
  {
    path: '/login',
    name: 'Login',
    component: () => import('../views/Login.vue')
  },
  {
    path: '/home',
    name: 'Home',
    component: () => import('../views/Home.vue'),
    redirect: '/home/basic-data',
    children: [
      {
        path: 'basic-data',
        name: 'BasicData',
        component: () => import('../views/BasicData.vue')
      },
      {
        path: 'activity-stats',
        name: 'ActivityStats',
        component: () => import('../views/ActivityStats.vue')
      },
      {
        path: 'users',
        name: 'Users',
        component: () => import('../views/Users.vue')
      },
      {
        path: 'products',
        name: 'Products',
        component: () => import('../views/Products.vue')
      },
      {
        path: 'config',
        name: 'Config',
        component: () => import('../views/Config.vue')
      },
      {
        path: 'proplog',
        name: 'PropLog',
        component: () => import('../views/PropLog.vue')
      },
      {
        path: 'taglog',
        name: 'TagLog',
        component: () => import('../views/TagLog.vue')
      },
      {
        path: 'player-info',
        name: 'PlayerInfo',
        component: () => import('../views/PlayerInfo.vue')
      },
      {
        path: 'payment',
        name: 'PaymentMain',
        component: () => import('../views/PaymentMain.vue'),
        redirect: '/home/payment/statistics',
        children: [
          {
            path: 'statistics',
            name: 'PaymentStatistics',
            component: () => import('../views/payment/PaymentStatistics.vue')
          },
          {
            path: 'records',
            name: 'PaymentRecords',
            component: () => import('../views/payment/PaymentRecords.vue')
          },
          {
            path: 'management',
            name: 'PaymentManagement',
            component: () => import('../views/payment/PaymentManagement.vue')
          }
        ]
      },
      {
        path: 'withdrawal',
        name: 'WithdrawalMain',
        component: () => import('../views/WithdrawalMain.vue'),
        redirect: '/home/withdrawal/statistics',
        children: [
          {
            path: 'statistics',
            name: 'WithdrawalStatistics',
            component: () => import('../views/withdrawal/WithdrawalStatistics.vue')
          },
          {
            path: 'records',
            name: 'WithdrawalRecords',
            component: () => import('../views/withdrawal/WithdrawalRecords.vue')
          },
          {
            path: 'management',
            name: 'WithdrawalManagement',
            component: () => import('../views/withdrawal/WithdrawalManagement.vue')
          }
        ]
      },
      {
        path: 'machine-stats',
        name: 'MachineStats',
        component: () => import('../views/MachineStats.vue'),
        redirect: '/home/machine-stats/history',
        children: [
          {
            path: 'history',
            name: 'MachineHistory',
            component: () => import('../views/machine-stats/MachineHistory.vue')
          },
          {
            path: 'detail',
            name: 'MachineDetail',
            component: () => import('../views/machine-stats/MachineDetail.vue')
          },
          {
            path: 'daily-profit',
            name: 'MachineDailyProfit',
            component: () => import('../views/machine-stats/MachineDailyProfit.vue')
          }
        ]
      },
      {
        path: 'online-info',
        name: 'OnlineInfo',
        component: () => import('../views/OnlineInfo.vue')
      },
      {
        path: 'invite',
        name: 'InviteMain',
        component: () => import('../views/InviteMain.vue'),
        redirect: '/home/invite/statistics',
        children: [
          {
            path: 'statistics',
            name: 'InviteStatistics',
            component: () => import('../views/invite/InviteStatistics.vue')
          },
          {
            path: 'records',
            name: 'InviteRecords',
            component: () => import('../views/invite/InviteRecords.vue')
          }
        ]
      },
      {
        path: 'cdkey-management',
        name: 'CdkeyManagement',
        component: () => import('../views/CdkeyManagement.vue')
      },
      {
        path: 'channel-management',
        name: 'ChannelManagement',
        component: () => import('../views/ChannelManagement.vue')
      }
    ]
  },
 
  {
    path: '*',
    redirect: '/login'
  }
]

const router = new VueRouter({
  routes
})

// Navigation guard
router.beforeEach((to, from, next) => {
  // If not going to login page, check if user is logged in
  if (to.path !== '/login') {
    const token = localStorage.getItem('token')
    if (!token) {
      next('/login')
    } else {
      next()
    }
  } else {
    next()
  }
})

export default router
